﻿using System;

namespace Faml.Frontend
{
    /// <summary>Location in source program</summary>
    public struct Location
    {
        private string source;
        private int line;
        private int col;

        public Location(string src, int line, int col)
        {
            this.source = src;
            this.line = line;
            this.col = col;
        }

        public Location(int line, int col)
        {
            this.source = String.Empty;
            this.line = line;
            this.col = col;
        }

        public Location(int col)
        {
            this.source = String.Empty;
            this.line = -1;
            this.col = col;
        }

        public static Location Unknown
        {
            get { return new Location(null, -1, -1); }
        }

        public bool IsUnkown
        {
            get { return source == null && line == -1 && col == -1; }
        }

        public override string ToString()
        {
            if (this.IsUnkown)
                return "?";

            if (line == -1)
                return col.ToString();
            else if (source == String.Empty)
                return String.Format("({0},{1})", line, col);
            else
                return String.Format("{0} at ({1},{2})", source, line, col);
        }
    }
}